package com.xzz.string;

/**
 * @author: hhz
 * @create: 2022-01-13 17:45
 * 5. 最长回文子串
 * 给你一个字符串 s，找到 s 中最长的回文子串。
 **/
public class LongestPalindrome {
    public static void main(String[] args) {
        System.out.println(longestPalindrome("babad"));
    }

    public static String longestPalindrome(String s) {
        String res = "";
        for (int i = 0; i < s.length(); i++) {
            String r1 = findString(s, i, i);
            String r2 = findString(s, i, i + 1);
            res = res.length()>r1.length()?res:r1;
            res = res.length()>r2.length()?res:r2;
        }
        return res;
    }
    public static String findString(String s,int i,int j){
        while (i>=0 && j<s.length() && s.charAt(i)==s.charAt(j)){
            i--;
            j++;
        }
        return s.substring(i+1,j);
    }
}
